import axios from 'axios';
import { config } from  '@/common/config.js'
import Vue from 'vue'
import httpRequest from '../utils/httpRequest'
import api from '../api'
Vue.prototype.$http = httpRequest // ajax请求方法
class AnalyticsService {
    constructor() {
        this.pageViews = this.loadPageViews() || {};
        // this.startDataTransmission();
    }

    loadPageViews() {
        const data = localStorage.getItem('pageViews');
        return data ? JSON.parse(data) : null;
    }

    savePageViews() {
        localStorage.setItem('pageViews', JSON.stringify(this.pageViews));
    }

    clearPageViews() {
        this.pageViews = {};
        this.savePageViews();
    }

    recordPageView(routeName) {
        if (!this.pageViews[routeName]) {
            this.pageViews[routeName] = 0;
        }
        this.pageViews[routeName] += 1;
        this.savePageViews();
    }

    getPageViews() {
        return this.pageViews;
    }

    async sendDataToBackend(routeName) {
        try {
            let param = { title: routeName, type: 'ZJ' };
            httpRequest.post(api.serveItemApi.gerRecordNum, param).then(res => {
            })
            // 清空缓存数据
            this.clearPageViews();
        } catch (error) {
            console.error('发生异常:', error);
            // 重新抛出异常
            throw error;
        }
    }

}

export default new AnalyticsService();
